package com.anjoyo.xgh.musicshare.db;

import java.util.ArrayList;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.anjoyo.xgh.musicshare.entry.ShareBean;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.util.JsonUtil;

public class MusicDBhelper {

	public static String TABLE_NAME = "musictable";
	public static String TABLE_NAME2 = "sharetable";
	MusicDB helper;
	SQLiteDatabase db;
	public MusicDBhelper(Context context) {
		helper = new MusicDB(context, "MUSIC.db", null, 1);
	}
	
	//收藏或下载歌曲信息添加
	public long saveMusic(Song song,boolean isfav,boolean isdown) {
		long id=0;
		db = helper.getWritableDatabase();
		Cursor c = db.query(TABLE_NAME, null, "name='"+song.getName()+"' and artist='"+song.getArtist()+"'", null, null, null, null);
		
		if (c.getCount()>0) {
//			System.out.println("变更");
			ContentValues values = new ContentValues();
			if (isfav) {
				values.put("isfav", 1);
			}
			if (isdown) {
				values.put("isdownload", 1);
			}
			id = db.update(TABLE_NAME,values, "musicid="+song.getMusicid(), null);
		}else {
			System.out.println("创建");
			ContentValues cv = new ContentValues();
			cv.put("musicid", song.getMusicid());
			cv.put("name", song.getName());
			cv.put("artist", song.getArtist());
			cv.put("album_image", song.getImage_url());
			cv.put("album", song.getAblum());
			cv.put("data", song.getLocal_url());
			cv.put("net_url", song.getNet_url());
			cv.put("isnetmusic", song.isIsnetMusic());
			cv.put("isoffline", song.isIsdoffline());
			cv.put("isfav", isfav);
			cv.put("isdownload", isdown);
			id = db.insert(TABLE_NAME, null, cv);
		}
		db.close();
		return id;
	}
	
	//得到下载的列表信息并解析
	public ArrayList<Song> getDownLoadMusic() {
		ArrayList<Song> songs =new ArrayList<Song>();
		db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE_NAME, null, "isdownload=1", null, null, null, null);
		Song song = null;
		while (c.moveToNext()) {
			song = new Song();
			song.setMusicid(Integer.parseInt(c.getString(c.getColumnIndex("musicid"))));
			song.setAblum(c.getString(c.getColumnIndex("album")));
			song.setName(c.getString(c.getColumnIndex("name")));
			song.setImage_url(c.getString(c.getColumnIndex("album_image")));
			song.setArtist(c.getString(c.getColumnIndex("artist")));
			song.setLocal_url(c.getString(c.getColumnIndex("data")));
			song.setNet_url(c.getString(c.getColumnIndex("net_url")));
			int isnetint = c.getInt(c.getColumnIndex("isnetmusic"));
			if (isnetint==0) {
				song.setIsnetMusic(false);
			}else {
				song.setIsnetMusic(true);
			}
			songs.add(song);
		}
		db.close();
		return songs;
	}
	//得到收藏的列表信息并解析
	public ArrayList<Song> getFavMusic() {
		ArrayList<Song> songs =new ArrayList<Song>();
		db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE_NAME, null, "isfav=1", null, null, null, null);
		Song song = null;
		while (c.moveToNext()) {
			song = new Song();
			song.setMusicid(Integer.parseInt(c.getString(c.getColumnIndex("musicid"))));
			song.setAblum(c.getString(c.getColumnIndex("album")));
			song.setName(c.getString(c.getColumnIndex("name")));
			song.setImage_url(c.getString(c.getColumnIndex("album_image")));
			song.setArtist(c.getString(c.getColumnIndex("artist")));
			song.setLocal_url(c.getString(c.getColumnIndex("data")));
			song.setNet_url(c.getString(c.getColumnIndex("net_url")));
			int isnetint = c.getInt(c.getColumnIndex("isnetmusic"));
			if (isnetint==0) {
				song.setIsnetMusic(false);
			}else {
				song.setIsnetMusic(true);
			}
			int isfav = c.getInt(c.getColumnIndex("isfav"));
			if (isfav==0) {
				song.setIsfav(false);
			}else {
				song.setIsfav(true);
			}
			int isoffline = c.getInt(c.getColumnIndex("isoffline"));
			if (isoffline==0) {
				song.setIsdoffline(false);
			}else {
				song.setIsdoffline(true);
			}
			
			songs.add(song);
		}
		db.close();
		return songs;
	}
	//按歌曲id删除下载列表信息
	public int deletsong(int id) {
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("isdownload", 0);
		values.put("isoffline", 0);
		int row = db.update(TABLE_NAME,values, "musicid="+id, null);
		db.close();
		return row;
	}
	//按歌曲id删除收藏列表信息
	public int deletFavsong(int id) {
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("isfav", 0);
		values.put("isoffline", 0);
		values.put("isdownload", 0);
		int row = db.update(TABLE_NAME,values, "musicid="+id, null);
		db.close();
		return row;
	}
	//删除全部网络歌曲收藏列表信息
	public int deletAllFavsong() {
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("isfav", 0);
		int row = db.update(TABLE_NAME,values, "isfav=1 and isnetmusic=1", null);
		db.close();
		return row;
	}
	//更新收藏歌曲离线状态
	public int updateoffline(int id,boolean isoffline) {
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("isoffline", isoffline);
		int row = db.update(TABLE_NAME,values, "musicid="+id, null);
		db.close();
		return row;
	}
	//离线（下载）完成,插入本地地址
	public int updatelocalurl(int id,String localurl) {
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("data", localurl);
		int row = db.update(TABLE_NAME,values, "musicid="+id, null);
		db.close();
		return row;
	}
	//得到收藏歌曲离线歌曲数目
	public int getFavofflineNum() {
		db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE_NAME, null, "isfav=1 and isoffline=1", null, null, null, null);
		int num = c.getCount();
		db.close();
		return num;
	}
	//按musicid查询歌曲是否是收藏过的
	public int isfav(Song song) {
		db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE_NAME, null, "isfav=1 and "+"name='"+song.getName()+"' and artist='"+song.getArtist()+"'", null, null, null, null);
		int num = c.getCount();
		db.close();
		return num;
	}
	
	
	/*----------------------------------------分享列表信息操作------------------------------------------*/
	//保存缓存的分享列表信息
		public long saveshare(String sharejson){
			long id=0;
			db = helper.getWritableDatabase();
			ContentValues cv = new ContentValues();
			cv.put("share",sharejson);
			id = db.insert(TABLE_NAME2, null, cv);
			db.close();
			return id;
		}
		//获得缓存的分享列表信息，并解析
		public ArrayList<ShareBean> getShareList() {
			ArrayList<ShareBean> beans = null;
			db = helper.getReadableDatabase();
			Cursor cursor = db.query(TABLE_NAME2, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				beans = JsonUtil.parseShareList(cursor.getString(cursor.getColumnIndex("share")));
			}
			db.close();
			return beans;
		}
		//删除缓存的分享列表信息
		public int deletShareList() {
			db = helper.getWritableDatabase();
			int row = db.delete(TABLE_NAME2, null, null);
			db.close();
			return row;
		}
		/*----------------------------------------分享列表信息操作------------------------------------------*/
}
